package org.tinygroup.uiml.config;

import java.awt.Rectangle;

import org.apache.commons.lang.StringUtils;
import org.tinygroup.uiml.metadata.ComponentType;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamOmitField;

/**
 * 组件
 * Created by luoguo on 2014/6/30.
 */
@XStreamAlias("component")
public class Component{
	
	public Component() {
		this(StringUtils.EMPTY);
	}
	
	public Component(String title){
		setTitle(title);
	}
	
	@XStreamAsAttribute
	public String width ;
	
	@XStreamAsAttribute
	public String height;
	
	
	@XStreamAsAttribute
	private String title;
	
    /**
     * 组件的ID，必须唯一，这里指UUID，且永远不能被改变
     */
	@XStreamAsAttribute
    private String id;
    /**
     * 继承的组组件ID，如果继承自多个组件，则用“,”号分隔
     */
	@XStreamAsAttribute
	@XStreamAlias("extend-ids")
    private String extendIds;
    /**
     * 组件引用ID，如果存在，则其内部不允许进行任何改动，即使设置了任何值，也会被忽略。
     */
	@XStreamAsAttribute
	@XStreamAlias("ref-id")
    private String refId;
    /**
     * 选用已经设置好的风格列表，如果有多个，用逗号分隔；如果设置的属性冲突，后面添加的覆盖前面的
     */
	@XStreamAlias("styles")
	@XStreamAsAttribute
	private String styles;
    /**
     * 组件类型
     */
	@XStreamAsAttribute
    private String type;
    /**
     * 私有样式，如果与上面引用的冲突，以当前设置为准
     */
	@XStreamAlias("private-style")
    private Properties uniqueStyle;
    /**
     * 组件的属性
     */
	@XStreamAlias("component-properties")
    private Properties properties;

	@XStreamOmitField
	private Component parent;
	
	@XStreamOmitField
	private ComponentType componentType;
	
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getExtendIds() {
        return extendIds;
    }

    public void setExtendIds(String extendIds) {
        this.extendIds = extendIds;
    }

    public String getRefId() {
        return refId;
    }

    public void setRefId(String refId) {
        this.refId = refId;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Properties getUniqueStyle() {
    	if (uniqueStyle == null) {
    		uniqueStyle = new Properties();
		}
		return uniqueStyle;
	}

	public void setUniqueStyle(Properties uniqueStyle) {
		this.uniqueStyle = uniqueStyle;
	}

	public String getStyles() {
		return styles;
	}

	public void setStyles(String styles) {
		this.styles = styles;
	}

	public Properties getProperties() {
		if (properties == null) {
			properties = new Properties();
		}
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

	public Component getParent() {
		return parent;
	}

	public void setParent(Component parent) {
		this.parent = parent;
	}

	public String getTitle() {
		return StringUtils.defaultString(title);
	}

	public void setTitle(String title) {
		this.title = title;
	}


	public ComponentType getComponentType() {
		return componentType;
	}

	public void setComponentType(ComponentType componentType) {
		this.componentType = componentType;
	}

	public void setLocation(Rectangle location) {
		this.width = String.valueOf(location.width);
		this.height = String.valueOf(location.height);
	}

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public String getWidth() {
		return width;
	}

	public void reload(){
		setWidth(getWidth());
		setHeight(getHeight());
	}
	
	public void setWidth(String width) {
		this.width = width;
	}

}
